package com.m3958.firstgwt.client.window;

import com.google.inject.Inject;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.firstgwt.client.IhasSectionArticleSelector;
import com.m3958.firstgwt.client.datasource.ArticleDataSource;
import com.m3958.firstgwt.client.datasource.SectionDataSource;
import com.m3958.firstgwt.client.types.SectionField;
import com.m3958.firstgwt.client.types.SmartSubOperationName;
import com.m3958.firstgwt.shared.type.ArticleField;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartParameterName;
import com.m3958.gwtutil.clientside.client.jso.ArticleJso;
import com.m3958.gwtutil.clientside.client.jso.SectionJso;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.SortSpecifier;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AnimationEffect;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;

public class SectionArticleSelector extends Window{
	
	private boolean initialized = false;
	
	
	@Inject
	private SectionDataSource sds;
	
	@Inject
	private AppStatus aservice;
	
	@Inject
	private ArticleDataSource ads;
	
	private TreeGrid sectionTreeGrid;
	
	private ListGrid articleGrid;
	
	private IhasSectionArticleSelector requester;
	
	private String siteId;
	
	private boolean forSection;
	
	public SectionArticleSelector(){
		setWidth(800);
		setHeight(600);
		setIsModal(true);
		setShowModalMask(true);
		setTitle("站点目录或者文章");
		centerInPage();
	}
	
	public SectionArticleSelector showMe(IhasSectionArticleSelector requester,String siteId,boolean forSection){
		this.requester = requester;
		this.siteId = siteId;
		this.forSection = forSection;
		if(!initialized){
			initialized = true;
			VLayout vl = new VLayout(5);
			vl.setWidth100();
			vl.setHeight100();
			HLayout hl = new HLayout();
			hl.setWidth100();
			hl.setHeight100();
			hl.addMember(initTreeGrid());
			hl.addMember(initGrid());
			vl.addMember(hl);
			vl.addMember(initBt());
			addItem(vl);
		}
		fetchChildren();
		show();
		return this;
	}
	
	private void fetchChildren(){
	    Criteria c = new Criteria();
	    c.addCriteria(SmartParameterName.SUB_OPERATION_TYPE, SmartSubOperationName.FETCH_CHILDREN.toString());
    	c.addCriteria(CommonField.SITE_ID.getEname(), siteId);
    	c.addCriteria(SmartParameterName.SORTBY,CommonField.POSITION.getEname());
	    c.addCriteria(CommonField.TIMEZONE.getEname(),aservice.getTimeZone());
	    sectionTreeGrid.filterData(c);
	}
	
	public void fetchGridData(String sectionId){
		Criteria c = new Criteria();
		c.addCriteria(SmartParameterName.RELATION_PROPERTY_NAME,SectionField.ARTICLES.getEname());
		c.addCriteria(SmartParameterName.RELATION_MODEL_NAME,SectionDataSource.className);
		c.addCriteria(SmartParameterName.SUB_OPERATION_TYPE, SmartSubOperationName.MANY_TO_MANY.toString());
		c.addCriteria(SmartParameterName.RELATION_MODEL_ID, sectionId);
		c.addCriteria(SmartParameterName.IS_MASTER,false);
		c.addCriteria(CommonField.SITE_ID.getEname(),siteId);
		c.addCriteria(CommonField.TIMEZONE.getEname(),aservice.getTimeZone());
		articleGrid.filterData(c);
	}
	
	private ListGrid initGrid() {
		articleGrid = new ListGrid();
	    articleGrid.setShowRowNumbers(true);
	    articleGrid.setAutoFetchTextMatchStyle(TextMatchStyle.SUBSTRING);
	    SortSpecifier ssf[] = {new SortSpecifier(CommonField.CREATED_AT.getEname(),SortDirection.DESCENDING)};
	    articleGrid.setInitialSort(ssf);
	    articleGrid.setShowFilterEditor(true);
	    articleGrid.setDataSource(ads);
	    articleGrid.setAutoFetchData(false);
	    articleGrid.setCanDragRecordsOut(true);
	    articleGrid.setEmptyMessage("请点击右边的目录来显示目录下的文章！");
	    articleGrid.setSelectionType(SelectionStyle.SIMPLE);
	    articleGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
	    
        articleGrid.setShowSelectionCanvas(true);  
        articleGrid.setAnimateSelectionUnder(true); 
        
	    Canvas selectionUnderCanvasProperties = new Canvas();  
        selectionUnderCanvasProperties.setAnimateShowEffect(AnimationEffect.FADE);  
        selectionUnderCanvasProperties.setAnimateFadeTime(1000);  
        selectionUnderCanvasProperties.setBackgroundColor("#ffff40");  
        articleGrid.setSelectionUnderCanvasProperties(selectionUnderCanvasProperties); 
        
	    
	    articleGrid.setDataPageSize(50);
	    
	    ListGridField titleField = new ListGridField(ArticleField.TITLE.getEname(),ArticleField.TITLE.getCname());

	    ListGridField flagField = new ListGridField(ArticleField.FLAG.getEname(),ArticleField.FLAG.getCname());
	    ListGridField publishedAtField = new ListGridField(ArticleField.PUBLISHED_AT.getEname(),ArticleField.PUBLISHED_AT.getCname());
	    
	    ListGridField pkField = new ListGridField(CommonField.ID.getEname());
	    pkField.setType(ListGridFieldType.INTEGER);
	    pkField.setHidden(true);
	    pkField.setWidth(80);
	    
	    ListGridField versionField = new TreeGridField("version");
	    versionField.setHidden(true);
	    
	    articleGrid.setFields(titleField,pkField,versionField,publishedAtField,flagField);
	    articleGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
			
			@Override
			public void onSelectionChanged(SelectionEvent event) {
				Record r = event.getRecord();
				if(r!=null){
					if(!forSection){
						requester.onArticleSelected((ArticleJso) r.getJsObj());
						SectionArticleSelector.this.hide();
					}
				}
			}
		});
	    return articleGrid;
	}
	
	private TreeGrid initTreeGrid() {
		sectionTreeGrid = new TreeGrid();
		sectionTreeGrid.setAutoFetchData(false);
		sectionTreeGrid.setTitleField(CommonField.NAME.getEname());
		sectionTreeGrid.setShowRoot(false);
		sectionTreeGrid.setCanAcceptDroppedRecords(true);
		sectionTreeGrid.setDataSource(sds);
		sectionTreeGrid.setSelectionType(SelectionStyle.SINGLE);
		sectionTreeGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
		
		sectionTreeGrid.setShowResizeBar(true);
		sectionTreeGrid.setWidth("30%");
		sectionTreeGrid.setHeight100();
		
		SortSpecifier ssf[] = {new SortSpecifier(CommonField.POSITION.getEname(),SortDirection.ASCENDING)};
		sectionTreeGrid.setSort(ssf);
		
        TreeGridField parentField = new TreeGridField(CommonField.PARENT_ID.getEname(),CommonField.PARENT_ID.getCname());
        parentField.setType(ListGridFieldType.INTEGER);
        parentField.setHidden(true);
        
	    ListGridField pkField = new ListGridField(CommonField.ID.getEname());
	    pkField.setType(ListGridFieldType.INTEGER);
	    pkField.setHidden(true);
	    pkField.setWidth(80);

        TreeGridField nameField = new TreeGridField(CommonField.NAME.getEname(),CommonField.NAME.getCname());
        sectionTreeGrid.setFields(nameField,pkField,parentField);
        sectionTreeGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
			
			@Override
			public void onSelectionChanged(SelectionEvent event) {
				Record r = event.getRecord();
				if(r!=null){
					if(forSection){
						requester.onSectionSelected((SectionJso) r.getJsObj());
						SectionArticleSelector.this.hide();
					}else{
						fetchGridData(r.getAttributeAsString(CommonField.ID.getEname()));
					}
				}
				
			}
		});
        return sectionTreeGrid;
	}

	private HLayout initBt() {
		HLayout hl = new HLayout();
		hl.setHeight(25);
		hl.setAlign(Alignment.RIGHT);
		IButton ib = new IButton("关闭");
		ib.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				SectionArticleSelector.this.hide();
			}
		});
		hl.addMember(ib);
		return hl;
	}
}
